package com.dji.compose_learning.ui.view

import android.util.Log
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import kotlinx.coroutines.delay

@Composable
fun LandingScreen(onTimeOut: () -> Unit) {
    val newOnTimeOut by rememberUpdatedState(onTimeOut) //记住这个写法！！！！！
    LaunchedEffect(Unit) {
        Log.i("buobao", "start launched effect1.")
        repeat(5) {
            delay(1000)
            Log.i("buobao", "1 times $it")
        }
        onTimeOut() //这个不会更新
        newOnTimeOut() //这个会更新
    }
}

@Composable
fun RememberUpdateView() {
    val timeOut1: () -> Unit = { Log.i("buobao", "timeOut1") }
    val timeOut2: () -> Unit = { Log.i("buobao", "timeOut2") }
    val changeOnTimeOutState = remember {
        mutableStateOf(timeOut1)
    }

    Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
        Column {
            Button(onClick = {
                if (changeOnTimeOutState.value == timeOut1) {
                    changeOnTimeOutState.value = timeOut2
                } else {
                    changeOnTimeOutState.value = timeOut1
                }
            }) {
                Text(text = if (changeOnTimeOutState.value == timeOut1) "timeout1" else "timeout2")
            }
            LandingScreen(onTimeOut = changeOnTimeOutState.value)
        }
    }
}